package com.yykj.user.center.server.controller.patient;

import com.github.wxpay.sdk.WXPayUtil;
import com.yykj.common.vo.ResultVO;
import com.yykj.user.center.server.service.patient.PaymentService;
import com.yykj.user.center.server.utils.ConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author chenl
 * @version 1.0
 * @date 2021/11/4 20:34
 */
@Slf4j
@Api(tags = "支付相关接口")
@RestController
@RequestMapping("/api/payment")
@SuppressWarnings("unchecked")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @ApiOperation("问诊支付")
    @ApiParam(name = "orderNo", value = "订单编号")
    @GetMapping("/consultataionOrder/pay/{orderNo}")
    public ResultVO pay(@PathVariable("orderNo") String orderNo) {
        return ResultVO.success(paymentService.consultationPay(orderNo));
    }

    @RequestMapping(value = "/notify/{orderNo}")
    public void notify(@PathVariable("orderNo") String orderNo) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            //1.输入流转换为字符串
            String xml = ConvertUtils.convertToString(request.getInputStream());
            log.info("微信支付回调信息 => {}", xml);
            //2.基于微信发送的通知内容,完成后续的业务逻辑处理
            //使用微信支付sdk中的工具类,将xml转换成map
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            if ("SUCCESS".equals(map.get("result_code"))) {
                //支付成功回调
                //给微信一个结果通知
                response.setContentType("text/xml");
                String data = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                response.getWriter().write(data);
//				paymentNotifySevice.notify(paymentNo,xml);
                paymentService.notify(orderNo, xml);
                //执行支付成功后的业务逻辑
                //例如：修改订单的支付状态为已支付
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/consultataionOrder/paysuccess/{orderNo}")
    public ResultVO success(@PathVariable("orderNo") String orderNo) {
        paymentService.success(orderNo);
        return ResultVO.success();
    }


    @GetMapping("/refund")
    @ApiOperation(value = "微信退款")
    public ResultVO refund(@RequestParam("order") String order) {
        paymentService.refund(order);
        return ResultVO.success();
    }

}
